package course3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OrderService {
    private String address;

    public Order creatOrder(OrderDishes[] orderDishes) throws NoAddressException {

        if (address == null) {
            throw new NoAddressException();
        }
        Order order = new Order();
        double totalAmount = 0d;

        for (OrderDishes orderDish : orderDishes) {
            //把每个菜品的个数和金额相×并叠加总计
            double amounnt = orderDish.getDishes().getPrice() * orderDish.getCount();
            totalAmount = totalAmount + amounnt;

        }

        order.setTotalAmount(totalAmount);
        order.setDishes(orderDishes);

        return order;

    }

    public void confirmOrder(Order order) {
        System.out.println("本次下单的收货地址是：" + address);
        System.out.println("-----------");
        System.out.println("本次下单的商品信息是");
        System.out.println("------");
        for (OrderDishes dish : order.getDishes()) {
            System.out.println("商品名称：" + dish.getDishes().getName());
            System.out.println("菜品价格：" + dish.getDishes().getPrice());
            System.out.println("菜品数量:" + dish.getCount());
            System.out.println("-------");
        }
        System.out.println("本次需要支付的总金额是:" + order.getTotalAmount());
    }

    public void payOrder(Order order) {

        boolean isTure = false;
        while (!isTure) {

            String str = null;
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
                str = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            double amount = Double.valueOf(str);
            if (amount == order.getTotalAmount()) {
                isTure = true;
            }
        }
    }


    public void setAddress(String address) {
        this.address = address;
    }
}

